package plus.chendd.jdk8;

import org.junit.Test;
import plus.chendd.entity.User;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {
  
  @Test
  public void test001() {
    List<String> list = Arrays.asList("张三", "张三丰", "成龙", "周星驰");
    
    list.stream().filter(s -> s.startsWith("张")).forEach(System.out::println);
  }
  
  @Test
  public void test002() {
    Stream.of(1, 2, 3, 4).filter(x -> x > 2).forEach(System.out::println);
    
    String[] arr1 = {"aa", "bb", "cc"};
    Stream<String> arr11 = Stream.of(arr1);
    arr11.forEach(System.out::println);
    
    Integer[] arr2 = {1, 2, 3, 4};
    Stream<Integer> arr21 = Stream.of(arr2);
    arr21.forEach(System.out::println);
    
    System.out.println(Stream.of(arr2).count());
  }
  
  @Test
  public void test003() {
    Arrays.asList("1", "2", "3").stream().limit(2).forEach(System.out::println);
  }
  
  @Test
  public void test004() {
    Arrays.asList("1,", "3", "4").stream().skip(2).forEach(System.out::println);
  }
  
  @Test
  public void test005() {
    Arrays.asList("1", "2", "3").stream().map(x -> new User(x, Integer.parseInt(x))).collect(Collectors.toList()).forEach(System.out::println);
  }
  
  @Test
  public void tes006() {
    Stream.of("1", "3", "2", "4", "0", "9", "7").map(Integer::parseInt).sorted((o1, o2) -> o2 - o1).forEach(System.out::println);
  }
  
  @Test
  public void test007() {
    Stream.of("1", "3", "2", "4", "0", "9", "9", "7").distinct().forEach(System.out::println);
  }
  
  @Test
  public void test008() {
    Stream<String> stringStream = Stream.of("1", "3", "2", "4", "0", "9", "9", "7");
    System.out.println(stringStream.map(Integer::parseInt).allMatch(x -> x > 0));
    Stream<String> stringStream1 = Stream.of("1", "3", "2", "4", "0", "9", "9", "7");
    System.out.println(stringStream1.map(Integer::parseInt).anyMatch(x -> x >= 9));
    Stream<String> stringStream2 = Stream.of("1", "3", "2", "4", "0", "9", "9", "7");
    System.out.println(stringStream2.map(Integer::parseInt).noneMatch(x -> x > 9));
  }
  
  @Test
  public void test009() {
    Optional<String> first = Stream.of("1", "3", "3", "4", "5", "1", "7").findFirst();
    System.out.println(first.get());
    
    Optional<String> any = Stream.of("1", "3", "3", "4", "5", "1", "7").findAny();
    System.out.println(any.get());
  }
  
  @Test
  public void test010() {
    Stream.of("1", "3", "3", "4", "5", "1", "7").map(Integer::parseInt).max((o1, o2) -> o1 - o2).ifPresent(System.out::println);
  }
  
  @Test
  public void test011() {
    int reduce1 = Stream.of(new User("张三", 18), new User("李四", 22), new User("张三", 13), new User("王五", 15), new User("张三", 19)).map(User::getAge).reduce(0, Integer::sum);
    System.out.println(reduce1);
  }
  
  @Test
  public void test012() {
    Integer reduce = Stream.of(new User("张三", 18), new User("李四", 22), new User("张三", 13), new User("王五", 15), new User("张三", 19)).map(User::getAge).reduce(0, Math::max);
    System.out.println(reduce);
  }
  
  @Test
  public void test013() {
    Integer reduce = Stream.of("a", "b", "c", "d", "a", "c", "a").map(x -> "a".equals(x) ? 1 : 0).reduce(0, Integer::sum);
    System.out.println(reduce);
    
    long count = Stream.of("a", "b", "c", "d", "a", "c", "a").filter(x -> "a".equals(x)).count();
    System.out.println(count);
  }
  
  @Test
  public void test014() {
    List<String> collect = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toList());
    collect.stream().forEach(System.out::println);
  }
  
  @Test
  public void test015() {
    Set<String> collect = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toSet());
    collect.stream().forEach(System.out::println);
  }
  
  @Test
  public void test016() {
    HashSet<String> collect = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toCollection(HashSet::new));
    collect.stream().forEach(System.out::println);
  }
  
  @Test
  public void test017() {
    LinkedList<String> collect = Stream.of("aa", "bb", "cc", "aa").collect(Collectors.toCollection(LinkedList::new));
    collect.stream().forEach(System.out::println);
  }
  
  @Test
  public void test018() {
    Object[] objects = Stream.of("aa", "bb", "cc", "aa").toArray();
    System.out.println(Arrays.toString(objects));
  }
  
  @Test
  public void test019() {
    Stream.of(new User("张三", 18), new User("李四", 22), new User("张三", 13), new User("王五", 15), new User("张三", 19)).collect(Collectors.maxBy((o1, o2) -> o1.getAge() - o2.getAge())).ifPresent(System.out::println);
  }
  
  @Test
  public void test020() {
    Integer collect = Stream.of(new User("张三", 18), new User("李四", 22), new User("张三", 13), new User("王五", 15), new User("张三", 19)).collect(Collectors.summingInt(User::getAge));
    System.out.println(collect);
  }
}
